package com.gxwy.shenzhou;

import com.gxwy.shenzhou.HeaderRequest;
import com.gxwy.shenzhou.SplitString;
import com.gxwy.shenzhou.StringHandler;
import com.gxwy.shenzhou.Supply;



public class ZhangHuXinXiChaXunRequest implements StringHandler{
private String sqgy;
private String fwfs;
private String mm;
private String zhdh;
private String ywdh;
private String hbzl;

private static final int SGGY_LEN=4;
private static final int FWFS_LEN=1;
private static final int ZHDH_LEN=32;
private static final int MM_LEN=16;
private static final int YWDH_LEN=3;
private static final int HBZL_LEN=2;

private static final int SGGY_FLAG=2;
private static final int FWFS_FLAG=2;
private static final int ZHDH_FLAG=2;
private static final int MM_FLAG=2;
private static final int YWDH_FLAG=2;
private static final int HBZL_FLAG=2;

private int reallen=0;
private HeaderRequest header=new HeaderRequest(false);

public HeaderRequest getHeader() {
	return header;
}
public void setHeader(HeaderRequest header) {
	this.header = header;
}
private static final int Total=SGGY_LEN+FWFS_LEN+ZHDH_LEN+MM_LEN+YWDH_LEN+HBZL_LEN;

public String getSqgy() {
	return sqgy;
}
public void setSqgy(String sqgy) throws Exception{
	if(sqgy==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(sqgy.getBytes("gbk").length<SGGY_LEN){

		this.sqgy=Supply.supply(sqgy,SGGY_FLAG,SGGY_LEN);

		
	}else if(sqgy.getBytes("gbk").length>SGGY_LEN){
		 Exception e=new Exception("Too long");
		 throw e;
	}else{
		this.sqgy = sqgy;			
	}
}
public String getFwfs() {
	return fwfs;
}
public void setFwfs(String fwfs) throws Exception{
	if(fwfs==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(fwfs.getBytes("gbk").length<FWFS_LEN){

		this.fwfs=Supply.supply(fwfs,FWFS_FLAG,FWFS_LEN);;
	}else if(fwfs.getBytes("gbk").length>FWFS_LEN){
		 Exception e=new Exception("Too long");
		 throw e;
	}else{
		this.fwfs = fwfs;			
	}
}
public String getZhdh() {
	return zhdh;
}
public void setZhdh(String zhdh) throws Exception{
		if(zhdh==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(zhdh.getBytes("gbk").length<ZHDH_LEN){
//		for(int i=0;i<ZHDH_LEN-fwfs.getBytes("gbk").length;i++){
//			zhdh=""+zhdh;
//		}
		this.zhdh=Supply.supply(zhdh,ZHDH_FLAG,ZHDH_LEN);;
	}else if(zhdh.getBytes("gbk").length>ZHDH_LEN){
		 Exception e=new Exception("Too long");
		 throw e;
	}else{
		this.zhdh = zhdh;			
	}
}
public String getMm() {
	return mm;
}
public void setMm(String mm) throws Exception{
	if(mm==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(mm.getBytes("gbk").length<MM_LEN){
//		for(int i=0;i<MM_LEN-fwfs.getBytes("gbk").length;i++){
//			mm=""+mm;
//		}
		this.mm=Supply.supply(mm,MM_FLAG,MM_LEN);

	}else if(mm.getBytes("gbk").length>MM_LEN){
		 Exception e=new Exception("Too long");
		 throw e;
	}else{
		this.mm = mm;			
	}
}


public String getYwdh() {
	return ywdh;
}
public void setYwdh(String ywdh) throws Exception{
	if(ywdh==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(ywdh.getBytes("gbk").length<YWDH_LEN){
//		for(int i=0;i<YWDH_LEN-ywdh.getBytes("gbk").length;i++){
//			ywdh=""+ywdh;
//		}
		

		this.ywdh=Supply.supply(ywdh,YWDH_FLAG,YWDH_LEN);;
	}else if(ywdh.getBytes("gbk").length>YWDH_LEN){
		 Exception e=new Exception("Too long");
		 throw e;
	}else{
		this.ywdh =ywdh;			
	}
}
public String getHbzl() {
	return hbzl;
}
public void setHbzl(String hbzl) throws Exception{
	if(hbzl==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(hbzl.getBytes("gbk").length<HBZL_LEN){
		this.hbzl=Supply.supply(hbzl,HBZL_FLAG,HBZL_LEN);;
	}else if(hbzl.getBytes("gbk").length>HBZL_LEN){
		 Exception e=new Exception("Too long");
		 throw e;
	}else{
		this.hbzl =hbzl;			
	}
}
public String build () throws Exception{
	if(this.getHeader()==null){
		throw new Exception("Header is null");
	}
	if(this.getSqgy()==null){
		this.setSqgy("");
	}
	if(this.getFwfs()==null){
		this.setFwfs("");
	}
	if(this.getZhdh()==null){
		this.setZhdh("");
	}
	if(this.getMm()==null){
		this.setMm("");
	}
	if(this.getYwdh()==null){
		this.setYwdh("");
	}
	if(this.getHbzl()==null){
		this.setYwdh("");
	}
	Integer totallen=header.TOTAL_LEN+Total-HeaderRequest.LEN_LEN;
    this.getHeader().setLen(totallen.toString());
		return 
				this.getHeader().build()+
				this.getSqgy()+this.getFwfs()+this.getZhdh()+this.getMm()+this.getYwdh()+this.getHbzl();
	}
public void parse(String str) throws Exception{
	if(str==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(str.getBytes("gbk").length<Total+header.TOTAL_LEN){
		Exception e=new Exception("Invalid request");
		throw e;
	}else{
		String headerLen=SplitString.splitString(str,reallen, reallen+header.TOTAL_LEN);
		reallen+=header.TOTAL_LEN;
		header.parse(headerLen);
		this.setSqgy(SplitString.splitString(str,reallen,reallen+SGGY_LEN));
	    reallen+=SGGY_LEN;
		this.setFwfs(SplitString.splitString(str,reallen,reallen+FWFS_LEN));
		reallen+=FWFS_LEN;
		this.setZhdh(SplitString.splitString(str,reallen,reallen+ZHDH_LEN));
		reallen+=ZHDH_LEN;
		this.setMm(SplitString.splitString(str,reallen,reallen+MM_LEN));
		reallen+=MM_LEN;
		this.setYwdh(SplitString.splitString(str,reallen,reallen+YWDH_LEN));
		reallen+=YWDH_LEN;
		this.setHbzl(SplitString.splitString(str,reallen,reallen+HBZL_LEN));
	}
	
}
  public static void main(String args[]) throws Exception {
  ZhangHuXinXiChaXunRequest build=new ZhangHuXinXiChaXunRequest();
//yes
	 build.setZhdh("4");
	 build.setYwdh("8");
	 build.setSqgy("23");
	 build.setMm("12");
	 build.setHbzl("6");
	 build.setFwfs("8");
	 String a;
	 a=build.build();
	 build.parse(a);
	 String ac=build.getHeader().build()+
				build.getSqgy()+build.getFwfs()+build.getZhdh()+build.getMm()+build.getYwdh()+build.getHbzl();
	 
	 if(build.build().equals(ac)){
		 System.out.println("�ܱ�yes");
	 }
	 if(build.getSqgy().trim().equals("4")){
		 System.out.println("yes");
	 }
	 if(build.getYwdh().trim().equals("8")){
		 System.out.println("Ywdhyes");
	 }
	 if(build.getMm().trim().equals("12")){
		 System.out.println("Mmyes");
	 }
	 if(build.getHbzl().trim().equals("6")){
		 System.out.println("Hbzlyes");
	 }
	 if(build.getFwfs().equals("8")){
		 System.out.println("Fwfsyes");
	 }
//	 System.out.println(build.getZhdh());
//	 System.out.println(build.getHbzl());
//	 System.out.println(build.getMm());
//	 System.out.println(build.getSqgy());
//  	String str="1   11                               1               1  1 ";
//  	build.parse(str);
//  	System.out.println(build.getFwfs());
//	System.out.println(build.getHbzl());
//	System.out.println(build.getMm());
//	System.out.println(build.getSqgy());
//	System.out.println(build.getYwdh());
//	System.out.println(build.getZhdh());
	 
  }
  }

